package top.jnaw.jee.pm.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import top.jnaw.jee.pm.model.Users;
import top.jnaw.jee.utils.Https;
import top.jnaw.jee.utils.Shiros;
import top.jnaw.jee.utils.nim.User;

public class UserController extends Controller {

  /**
   * 登录 username 用户名 password 密码
   */

  public void login() {
    final String username = getPara("username", "");
    final String password = getPara("password", "");
    boolean aa = Shiros.login(username, password);
    if (aa) {
      renderJson(Https.success("登录成功", ""));
    } else {
      renderJson(Https.failure("登录失败", ""));
    }
  }

  /**
   * 登出
   */
  public void logout() {
    if (Shiros.isAuthc()) {
      Shiros.logout();
    }
    renderJson(Https.success("退出成功", ""));
  }

  /**
   * 获取用户基本信息
   */
//  @RequiresAuthentication
//  @RequiresPermissions({"user:get"})
  public void get() {

    final int uid = getParaToInt("uid", -1);
    Users users = new Users();
    if (uid > -1) {
      users = Users.dao.findById(uid);

    }

    if (users != null) {
      renderJson(Https.success("查询成功", users));
    } else {
      renderJson(Https.failure("该用户不存在", ""));
    }
  }

  /**
   * 添加注册用户
   */
  public void add() {
    String username = getPara("username", "");
    String password = getPara("password", "");
    String name = getPara("name", "");

    boolean result = false;
    String msg = "注册失败! ";
    if (StrKit.notBlank(username, password, name)) {
      if (Users.getUserInfo2(username) == null) {
        result = Users.addUser(username, password, name);
      } else {
        msg += " 用户名已存在";
      }
    }

    if (result) {
      renderJson(Https.success("注册成功", ""));
    } else {
      renderJson(Https.failure(msg, ""));
    }

  }


  /**
   * 登录加获取用户信息
   */

  public void getUserInfo() {
    final String username = getPara("username", "");
    final String password = getPara("password", "");
    boolean result = false;
    if (StrKit.notBlank(username, password)) {
      result = Shiros.login(username, password);
    }

    if (result) {
      //返回信息
      Record users = Users.getUserInfo2(username);
      renderJson(Https.success("", users));
    } else {
      renderJson(Https.failure("用户名或密码错误", ""));
    }

  }

  /**
   * 修改用户
   */
  public void set() {
    final int uid = getParaToInt("uid", -1);
    String password = getPara("password", "");
    String name = getPara("name", "");

    boolean result = false;
    String msg = "修改失败! ";
    if (uid > -1) {
      Users users = Users.dao.findById(uid);
      if (users != null) {

        if (StrKit.notBlank(name)) {
          users.setName(name);
        }
        if (StrKit.notBlank(password)) {
          String pwd = Shiros.hashPassword(users.getUsername(), password);
          users.setPassword(pwd);
        }
        result = users.update();
      } else {
        msg += " 用户不存在";
      }
    }

    if (result) {
      renderJson(Https.success("修改成功", ""));
    } else {
      renderJson(Https.failure(msg, ""));
    }

  }

  /**
   * 修改密码，谁写的，set方法有这个功能
   */
  public void updatePassword(){
    String username=getPara("username","");
    String password=getPara("password","");
    boolean flag=false;
    String msg="";
    Users u=new Users();
    if(StrKit.notBlank(username)){
      u=Users.dao.findFirst("select * from users where username=?",username);
      if(u==null) {
        msg += "用户不存在";
      }
    }
    if(StrKit.notBlank(password)){
      String pwd = Shiros.hashPassword(u.getUsername(), password);
      u.setPassword(pwd);
      flag=u.update();
      if(!flag){
        msg+="修改失败";
      }
    }
    if(flag){
      renderJson(Https.success("修改成功",""));
    }else{
      renderJson(Https.failure(msg,""));
    }
  }
  public void getList() {
    List<Users> usersList = Users.getUserList();
    renderJson(Https.success("查询成功", usersList));
  }
  public void getUserList(){
    String username=getPara("username","");
    renderJson(Https.success("查询成功",Users.dao.find("select * from users where name like '%"+username+"%' and username <> 'admin'" )));
  }
  public  void rm(){
    final int uid = getParaToInt("uid", -1);
    boolean res=false;
    if (uid>-1){
      Users u =Users.dao.findById(uid);
      if (u!=null){

        res=u.delete();
      }
    }
    if (res){
      renderJson(Https.success("删除成功", ""));
    }else{
      renderJson(Https.failure("删除失败",""));
    }

  }
}
